﻿import QtQuick 2.12
import "../components"

Rectangle {
    id: root
    height: parent.height
    width: parent.width
    border.color: "#11111111"

    property real pagePadding: 20*dp
    property var listData: [[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]]
    property var columnScale: [0.17, 0.25, 0.25, 0.18, 0.17]
    property var titleList: ["序号", "名称", "完成日期", "类型", "操作"]

    property real lineHeight: 40*dp

    property int checkedIdx: 1          // 这里用的是数据库id

    function initCheckedState(){
        listData = CGetMessageInterface.getReportInfo()
        if(listData.length === 0){
            checkedIdx = -1
        }else{
            checkedIdx = listData[0][0]
        }
        CReport.setReportIdx(checkedIdx)
        refreshPrinterReview()
    }

    function refreshHistoryData(){
        listData = CGetMessageInterface.getReportInfo()
        if(listData.length === 0){
            checkedIdx = -1
        }
    }

    // 标题栏
    Item{
        id: layout_title
        x: pagePadding; y: pagePadding
        height: 40*dp
        width: parent.width - 2*x
        Text{
            text: qsTr("历史报告")
            font.bold: true
            font.pixelSize: config.fontsize_1
            anchors.verticalCenter: parent.verticalCenter
        }
        CustomButton{
            button_icon: "qrc:/picture/pzwh_sc2.png"
            x: parent.width - width
            title: "全部删除"
            defaultMode3: true
            width: 100*dp
            CustomPopover{
                title: "确认删除全部历史记录吗?"
                onAccepted: {
                    CGetMessageInterface.delReportInfo(0)
                    checkedIdx = -1
                    refreshHistoryData()
                    checkedIdx = CReport.setReportIdx(-1)
                    refreshPrinterReview()
                }
            }
        }
    }


    // 表头
    Item{
        id: layout_tableTitle
        x: pagePadding; y: layout_title.y + layout_title.height
        height: 40*dp
        width: parent.width - x*2
        Row{
            Repeater{
                model: titleList.length
                Text{
                    text: titleList[index]
                    color: "#999999"
                    height: 40*dp; width: columnScale[index]*layout_tableTitle.width
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: config.fontsize
                }
            }
        }
        Rectangle{ height: 1; width: parent.width; color: "#DDDDDD" }
        Rectangle{ height: 1; width: parent.width; y: parent.height - height; color: "#DDDDDD" }
    }

    // 表信息
    ListView{
        id: listView_tableList
        clip: true
        x: pagePadding
        y: layout_tableTitle.y + layout_tableTitle.height
        height: parent.height - y - pagePadding
        width: parent.width - 2*x
        model: listData.length
        delegate: tableLine

    }

    Component{
        id: tableLine
        Item{
            id: item
            property int lineIdx: index
            property bool isHovered: false
            height: 40*dp; width: listView_tableList.width
            Row{
                id: row_line
                Repeater{
                    model: listData[item.lineIdx].length
                    Text{
                        text: {
                            if(index === 0){
                                return item.lineIdx + 1
                            }else if(index === 2){
                                if(listData[item.lineIdx][index]===""){
                                    return "实验异常结束"
                                }
                                return listData[item.lineIdx][index].split(" ")[0]
                            }
                            return listData[item.lineIdx][index]
                        }
                        color: {
                            if(text==="实验异常结束" && index===2)   return "#CCC"
                            else  return "#000"
                        }
                        height: 40*dp; width: columnScale[index]*layout_tableTitle.width
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        font.pixelSize: config.fontsize
                    }
                }
            }
            Rectangle{          // 分割线
                height: 1; width: parent.width
                y: parent.height - height
                color: "#DDDDDD"
            }Rectangle{
                anchors.fill: parent
                color: Qt.rgba(0.1, 0.7, 1, 0.08)
                visible: item.isHovered || checkedIdx === listData[item.lineIdx][0]
            }MouseArea{
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                hoverEnabled: true
                onEntered: {
                    item.isHovered = true
                }onExited: {
                    item.isHovered = false
                }
                onClicked: {
                    checkedIdx = Number(listData[item.lineIdx][0])
                    CReport.setReportIdx(checkedIdx)
                    refreshPrinterReview()
                }
            }
            // 删除按钮
            CustomImage{
                height: 20*dp; width: 20*dp
                anchors.verticalCenter: parent.verticalCenter
                source: "qrc:/picture/pz_sc.png"
                x: row_line.width + (columnScale[4]*layout_tableTitle.width - width) * 0.5
                MouseArea{
                    anchors.fill: parent
                    cursorShape: Qt.PointingHandCursor
                    hoverEnabled: true
                    onEntered: {
                        item.isHovered = true
                    }onExited: {
                        item.isHovered = false
                    }
                }
                CustomPopover{
                    title: "确认删除实验【"+listData[item.lineIdx][1]+"】吗?"
                    onAccepted: {
                        console.log("删除实验", listData[item.lineIdx][0])
                        if(checkedIdx === listData[item.lineIdx][0]){       // 如果删除的实验正好是当前选择的
                            checkedIdx = -1
                        }
                        CGetMessageInterface.delReportInfo(listData[item.lineIdx][0])
                        checkedIdx = CReport.setReportIdx(checkedIdx)
                        refreshPrinterReview()
                        refreshHistoryData()
                    }
                }
            }
        }
    }



}
